﻿using Microsoft.AspNetCore.Mvc;
using ZSZ.AdminWeb.Models;
using ZSZ.Application;
using ZSZ.CommonMVC;
using ZSZ.DTO;
using ZSZ.EntityFramework;

namespace ZSZ.AdminWeb.Controllers
{
    public class HouseController : Controller
    {
        private readonly UnitOfWork unitOfWork = null;
        public HouseController(ZSZDbContext context)
        {
            unitOfWork = new UnitOfWork(context);
        }
        //public IAdminUserService userSerivce { get; set; }

        //public IHouseService houseService { get; set; }
        //public ICityService cityService { get; set; }
        //public IRegionService regionService { get; set; }
        //public ICommunityService communityService { get; set; }
        //public IIdNameService idNameService { get; set; }
        //public IAttachmentService attService { get; set; }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="typeId">房源类型（整租、合租）</param>
        /// <returns></returns>
        [CheckPermission("House.List")]
        public ActionResult List(long typeId,int pageIndex=1)
        {            
            //因为AuthorizFilter做了是否登录的检查，因此这里不会取不到id
            long userId = (long)AdminHelper.GetUserId(HttpContext);
            long? cityId = unitOfWork.AdminUserService.GetById(userId).CityId;
            if (cityId == null)
            {
                //如果“总部不能***”的操作很多，也可以定义成一个AuthorizeFilter
                //最好用FilterAttribute的方式标注，这样对其他的不涉及这个问题的地方效率高
                //立即实现
                return View("Error", (object)"总部不能进行房源管理");
            }
            var houses = unitOfWork.HouseService.GetPagedData(cityId.Value, typeId, 10, (pageIndex-1)*10);
            long totalCount = unitOfWork.HouseService.GetTotalCount(cityId.Value, typeId);
            ViewBag.pageIndex = pageIndex;
            ViewBag.totalCount = totalCount;
            ViewBag.typeId = typeId;
            return View(houses);
        }

        public ActionResult LoadCommunities(long regionId)
        {
            var communities = unitOfWork.CommunityService.GetByRegionId(regionId);
            return Json(new AjaxResult { Status = "ok", Data = communities });
        }

        [CheckPermission("House.Add")]
        [HttpGet]
        public ActionResult Add()
        {
            long? userId = AdminHelper.GetUserId(HttpContext);
            long? cityId = unitOfWork.AdminUserService.GetById(userId.Value).CityId;
            if (cityId == null)
            {
                return View("Error", (object)"总部不能进行房源管理");
            }
            var regions = unitOfWork.RegionService.GetAll(cityId.Value);
            var roomTypes = unitOfWork.IdNameService.GetAll("户型");
            var statuses = unitOfWork.IdNameService.GetAll("房屋状态");
            var decorateStatuses = unitOfWork.IdNameService.GetAll("装修状态");
            var attachments = unitOfWork.AttachmentService.GetAll();
            var types = unitOfWork.IdNameService.GetAll("房屋类别");

            var model = new HouseAddViewModel
            {
                regions = regions,
                roomTypes = roomTypes,
                statuses = statuses,
                decorateStatuses = decorateStatuses,
                attachments = attachments,
                types = types
            };
            return View(model);
        }

        [ValidateAntiForgeryToken]
        [CheckPermission("House.Add")]
        [HttpPost]
        public ActionResult Add(HouseAddModel model)
        {
            long? userId = AdminHelper.GetUserId(HttpContext);
            long? cityId = unitOfWork.AdminUserService.GetById(userId.Value).CityId;
            if (cityId == null)
            {
                return View("Error", (object)"总部不能进行房源管理");
            }

            var dto = new HouseAddNewDTO
            {
                Address = model.address,
                Area = model.area,
                AttachmentIds = model.attachmentIds,
                CheckInDateTime = model.checkInDateTime,
                CommunityId = model.CommunityId,
                DecorateStatusId = model.DecorateStatusId,
                Description = model.description,
                Direction = model.direction,
                FloorIndex = model.floorIndex,
                LookableDateTime = model.lookableDateTime,
                MonthRent = model.monthRent,
                OwnerName = model.ownerName,
                OwnerPhoneNum = model.ownerPhoneNum,
                RoomTypeId = model.RoomTypeId,
                StatusId = model.StatusId,
                TotalFloorCount = model.totalFloor,
                TypeId = model.TypeId
            };

            long houseId= unitOfWork.HouseService.AddNew(dto);
            //CreateStaticPage(houseId); //生成静态页面           

            //生成房源查看的html文件
            return Json(new AjaxResult { Status = "ok" });
        }

        //private void CreateStaticPage(long houseId)
        //{
        //    var house = unitOfWork.HouseService.GetById(houseId);
        //    var pics = unitOfWork.HouseService.GetPics(houseId);
        //    var attachments = unitOfWork.AttachmentService.GetAttachments(houseId);

        //    HouseIndexViewModel model = new HouseIndexViewModel();
        //    model.House = house;
        //    model.Pics = pics;
        //    model.Attachments = attachments;
        //    string html = MVCHelper.RenderViewToString(this.ControllerContext, @"~/Views/House/StaticIndex.cshtml",
        //        model);
        //    System.IO.File.WriteAllText(@"E:\快盘\NextBig\NET课程\NET掌上租\上课代码\ZSZ\ZSZ.FrontWeb\"+houseId+".html", html);
        //}

        [CheckPermission("House.Edit")]
        [HttpGet]
        public ActionResult Edit(long id)
        {
            long? userId = AdminHelper.GetUserId(HttpContext);
            long? cityId = unitOfWork.AdminUserService.GetById(userId.Value).CityId;
            if (cityId == null)
            {
                return View("Error", (object)"总部不能进行房源管理");
            }
            var house = unitOfWork.HouseService.GetById(id);
            HouseEditViewModel model = new HouseEditViewModel();
            model.house = house;

            var regions = unitOfWork.RegionService.GetAll(cityId.Value);
            var roomTypes = unitOfWork.IdNameService.GetAll("户型");
            var statuses = unitOfWork.IdNameService.GetAll("房屋状态");
            var decorateStatuses = unitOfWork.IdNameService.GetAll("装修状态");
            var attachments = unitOfWork.AttachmentService.GetAll();
            var types = unitOfWork.IdNameService.GetAll("房屋类别");

            model.regions = regions;
            model.roomTypes = roomTypes;
            model.statuses = statuses;
            model.decorateStatuses = decorateStatuses;
            model.attachments = attachments;
            model.types = types;
            return View(model);
        }

        [CheckPermission("House.Edit")]
        [HttpPost]
        public ActionResult Edit(HouseEditModel model)
        {
            HouseDTO dto = new HouseDTO
            {
                Address = model.address,
                Area = model.area,
                AttachmentIds = model.attachmentIds,
                CheckInDateTime = model.checkInDateTime,
                //有没有感觉强硬用一些不适合的DTO，有一些没用的属性时候的迷茫？
                CommunityId = model.CommunityId,
                DecorateStatusId = model.DecorateStatusId,
                Description = model.description,
                Direction = model.direction,
                FloorIndex = model.floorIndex,
                Id = model.Id,
                LookableDateTime = model.lookableDateTime,
                MonthRent = model.monthRent,
                OwnerName = model.ownerName,
                OwnerPhoneNum = model.ownerPhoneNum,
                RoomTypeId = model.RoomTypeId,
                StatusId = model.StatusId,
                TotalFloorCount = model.totalFloor,
                TypeId = model.TypeId
            };
            unitOfWork.HouseService.Update(dto);

            //CreateStaticPage(model.Id);//编辑房源的时候重新生成静态页面
            return Json(new AjaxResult { Status="ok"});
        }

        public ActionResult PicUpload(int houseId)
        {
            return View(houseId);
        }

        //public ActionResult UploadPic(int houseId, HttpPostedFileBase file)
        //{
        //    /*
        //    if (houseId < 5)
        //    {
        //        return Json(new AjaxResult { Status = "error", ErrorMsg = "id必须大于5" });
        //    }*/
        //    //month月，minute
        //    string md5= CommonHelper.CalcMD5(file.InputStream);
        //    string ext = Path.GetExtension(file.FileName);
        //    string path = "/upload/" + DateTime.Now.ToString("yyyy/MM/dd") + "/" + md5 + ext;// /upload/2017/07/07/afadsfa.jpg
        //    string thumbPath = "/upload/" + DateTime.Now.ToString("yyyy/MM/dd") + "/" + md5+"_thumb" + ext;
        //    string fullPath = HttpContext.Server.MapPath("~" + path);//d://22/upload/2017/07/07/afadsfa.jpg
        //    string thumbFullPath = HttpContext.Server.MapPath("~" + thumbPath);
        //    new FileInfo(fullPath).Directory.Create();//尝试创建可能不存在的文件夹

        //    file.InputStream.Position = 0;//指针复位
        //    //file.SaveAs(fullPath);//SaveAs("d:/1.jpg");
        //    //缩略图
        //    ImageProcessingJob jobThumb = new ImageProcessingJob();
        //    jobThumb.Filters.Add(new FixedResizeConstraint(200, 200));//缩略图尺寸200*200
        //    jobThumb.SaveProcessedImageToFileSystem(file.InputStream, thumbFullPath);

        //    file.InputStream.Position = 0;//指针复位

        //    //水印
        //    ImageWatermark imgWatermark =
        //        new ImageWatermark(HttpContext.Server.MapPath("~/images/watermark.jpg"));
        //    imgWatermark.ContentAlignment = System.Drawing.ContentAlignment.BottomRight;//水印位置
        //    imgWatermark.Alpha = 50;//透明度，需要水印图片是背景透明的png图片
        //    ImageProcessingJob jobNormal = new ImageProcessingJob();
        //    jobNormal.Filters.Add(imgWatermark);//添加水印
        //    jobNormal.Filters.Add(new FixedResizeConstraint(600, 600));
        //    jobNormal.SaveProcessedImageToFileSystem(file.InputStream, fullPath);

        //    houseService.AddNewHousePic(new HousePicDTO {HouseId=houseId,Url= path,ThumbUrl= thumbPath });

        //    CreateStaticPage(houseId);//上传了新图片或者删除图片都要重新生成html页面

        //    return Json(new AjaxResult
        //    {
        //        Status = "ok"
        //    });
        //}

        public ActionResult PicList(long id)
        {
            var pics = unitOfWork.HouseService.GetPics(id);
            return View(pics);
        }

        public ActionResult DeletePics(long[] selectedIds)
        {         
            foreach(var picId in selectedIds)
            {
                unitOfWork.HouseService.DeleteHousePic(picId);
            }

            //CreateStaticPage(houseId);//上传了新图片或者删除图片都要重新生成html页面
            //不建议删除图片
            return Json(new AjaxResult { Status="ok"});
        }

        //public ActionResult RebuildAllStaticPage()
        //{
        //    var houses = unitOfWork.HouseService.GetAll();
        //    foreach(var house in houses)
        //    {
        //        CreateStaticPage(house.Id);
        //    }
        //    return Json(new AjaxResult { Status="ok"});
        //}
    }
}